package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryBusAppDocRelaReq;
import com.sg.dto.base.app.req.QueryAppFileRelationshipTestListReq;
import com.sg.dto.base.app.req.QueryBusAppDocRelaDetailReq;
import com.sg.dto.base.app.req.QueryBusAppDocRelaListReq;
import com.sg.entity.OmsBasicInstanceUnstructuredRelationship;
import com.sg.service.base.app.MOmsBasicInstanceUnstructuredRelationshipService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsBasicInstanceUnstructuredRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsBasicInstanceUnstructuredRelationshipServiceImpl
        extends ServiceImpl<OmsBasicInstanceUnstructuredRelationshipMapper, OmsBasicInstanceUnstructuredRelationship>
        implements MOmsBasicInstanceUnstructuredRelationshipService {

    @Autowired
    OmsBasicInstanceUnstructuredRelationshipMapper omsBasicInstanceUnstructuredRelationshipMapper;

    /**
     * 3-13-05批量删业务应用文档关系[2910]
     * gen by moon at 10/15/2022, 5:44:44 PM
     */
    @Trace(operationName = "3-13-05批量删业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteBusAppDocRela(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-13-05修改业务应用文档关系[2868]
     * gen by moon at 10/15/2022, 5:44:43 PM
     */
    @Trace(operationName = "3-13-05修改业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBusAppDocRela(OmsBasicInstanceUnstructuredRelationship omsBasicInstanceUnstructuredRelationship) {
        omsBasicInstanceUnstructuredRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBasicInstanceUnstructuredRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsBasicInstanceUnstructuredRelationship);
    }

    /**
     * 3-13-05批量查询业务应用文档关系[2779]
     * gen by moon at 10/15/2022, 5:44:42 PM
     */
    @Trace(operationName = "3-13-05批量查询业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBasicInstanceUnstructuredRelationship> batchQueryBusAppDocRela(BatchQueryBusAppDocRelaReq reqDto) {

        try {
            QueryWrapper<OmsBasicInstanceUnstructuredRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getBusAppDocRelaList() == null || reqDto.getBusAppDocRelaList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getBusAppDocRelaList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBasicInstanceUnstructuredRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsBasicInstanceUnstructuredRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-05新增业务应用文档关系[2698]
     * gen by moon at 11/13/2023, 4:48:36 PM
     */
    @Trace(operationName = "3-13-05新增业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBusAppDocRela(OmsBasicInstanceUnstructuredRelationship omsBasicInstanceUnstructuredRelationship) {

        // TODO: 2024/1/23
//     if(!StringUtils.isEmpty(omsBasicInstanceUnstructuredRelationship.getFileRelativePathAccess())){
//         omsBasicInstanceUnstructuredRelationship.setFileRelativePathAccess(CommonFunctionHelper.replaceFilePath(omsBasicInstanceUnstructuredRelationship.getFileRelativePathAccess()));
//     }
        omsBasicInstanceUnstructuredRelationship.setUnstructuredDataRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsBasicInstanceUnstructuredRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBasicInstanceUnstructuredRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBasicInstanceUnstructuredRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBasicInstanceUnstructuredRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBasicInstanceUnstructuredRelationship.setIsValid("FALSE");
        omsBasicInstanceUnstructuredRelationship.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsBasicInstanceUnstructuredRelationship.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsBasicInstanceUnstructuredRelationship)) {
            return omsBasicInstanceUnstructuredRelationship.getUnstructuredDataRelationshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-13-05查业务应用文档关系列表[2517]
     * gen by moon at 1/23/2024, 6:15:41 PM
     */
    @Trace(operationName = "3-13-05查业务应用文档关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBasicInstanceUnstructuredRelationship> queryBusAppDocRelaList(QueryBusAppDocRelaListReq reqDto) {

        try {
            QueryWrapper<OmsBasicInstanceUnstructuredRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBasicInstanceUnstructuredRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsBasicInstanceUnstructuredRelationship>) super.list(queryWrapper)).stream().map(item -> {
                item.setFileRelativePathAccess(CommonFunctionHelper.getFilePath(item.getFileRelativePathAccess()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-05删除业务应用文档关系[3239]
     * gen by moon at 10/15/2022, 5:44:44 PM
     */
    @Trace(operationName = "3-13-05删除业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBusAppDocRela(String unstructuredDataRelationshipId) {

        return super.removeById(unstructuredDataRelationshipId);
    }

    /**
     * 3-13-05批量修改业务应用文档关系[3340]
     * gen by moon at 10/15/2022, 5:44:45 PM
     */
    @Trace(operationName = "3-13-05批量修改业务应用文档关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateBusAppDocRela(List<OmsBasicInstanceUnstructuredRelationship> omsBasicInstanceUnstructuredRelationship) {

        if (omsBasicInstanceUnstructuredRelationship == null || omsBasicInstanceUnstructuredRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsBasicInstanceUnstructuredRelationship);

    }

    /**
     * M3-13-05一键删除业务应用文档关系测试验证数据[4432]
     * gen by moon at 10/15/2022, 5:44:46 PM
     */
    @Trace(operationName = "M3-13-05一键删除业务应用文档关系测试验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean autoDeleteAppFileRelationshipTest(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M3-13-05查询业务应用文档关系测试列表[4452]
     * gen by moon at 10/15/2022, 5:44:46 PM
     */
    @Trace(operationName = "M3-13-05查询业务应用文档关系测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBasicInstanceUnstructuredRelationship> queryAppFileRelationshipTestList(QueryAppFileRelationshipTestListReq reqDto) {

        try {
            QueryWrapper<OmsBasicInstanceUnstructuredRelationship> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBasicInstanceUnstructuredRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBasicInstanceUnstructuredRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsBasicInstanceUnstructuredRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-05批量增文档附件关系[6061]
     * gen by moon at 11/9/2023, 10:15:06 PM
     */
    @Trace(operationName = "3-13-05批量增文档附件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddDmAttRela(List<OmsBasicInstanceUnstructuredRelationship> omsBasicInstanceUnstructuredRelationship) {

        if (omsBasicInstanceUnstructuredRelationship == null || omsBasicInstanceUnstructuredRelationship.size() == 0) {
            return false;
        }

        return super.saveBatch(omsBasicInstanceUnstructuredRelationship.stream().map(item -> {

            // TODO: 2024/1/23
//          if(!StringUtils.isEmpty(item.getFileRelativePathAccess())){
//              item.setFileRelativePathAccess(CommonFunctionHelper.replaceFilePath(item.getFileRelativePathAccess()));
//          }
            item.setUnstructuredDataRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-13-05查询业务应用文档关系详情[2844]
     * gen by moon at 1/23/2024, 6:15:43 PM
     */
    @Trace(operationName = "3-13-05查询业务应用文档关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsBasicInstanceUnstructuredRelationship queryBusAppDocRelaDetail(QueryBusAppDocRelaDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsBasicInstanceUnstructuredRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsBasicInstanceUnstructuredRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsBasicInstanceUnstructuredRelationship retData = retList.get(0);
                retData.setFileRelativePathAccess(CommonFunctionHelper.getFilePath(retData.getFileRelativePathAccess()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
